<script setup lang="ts">
//@ts-ignore
import { ref, computed, watch } from 'vue';
//@ts-ignore
import Header from '../components/Header.vue';
//@ts-ignore
import Footer from '../components/Footer.vue';
import { useRouter } from 'vue-router';

const router = useRouter();

// 目的地数据
const destinations = ref([
  { 
    id: 1, 
    name: '巴黎', 
    description: '浪漫之都，艺术与美食的天堂', 
    image: 'https://ts1.tc.mm.bing.net/th/id/R-C.483d7a1f4518b81e410acd0ee269573b?rik=sADj5uCEh7piZQ&riu=http%3a%2f%2fn.sinaimg.cn%2fsinacn20103%2f560%2fw1080h1080%2f20190427%2ff8aa-hvvuiyp2070947.jpg&ehk=OQe10sDGIDAlUXp8mcA2L%2fqVmtZo%2fvSkq3Sd7jf3RBU%3d&risl=&pid=ImgRaw&r=0',
    price: '¥ 8,999',
    rating: 4.8,
    region: 2, // 欧洲
    themes: [2, 4], // 古城文化, 美食之旅
    activities: ['埃菲尔铁塔', '卢浮宫', '凯旋门', '塞纳河游船', '巴黎圣母院']
  },
  { 
    id: 2, 
    name: '圣托里尼', 
    description: '爱琴海上的蓝白小岛', 
    image: 'https://tse3-mm.cn.bing.net/th/id/OIP-C.k1tRYHLjA0cDir1EBuOWqQHaFj?rs=1&pid=ImgDetMain',
    price: '¥ 12,999',
    rating: 4.9,
    region: 2, // 欧洲
    themes: [1, 2], // 海岛度假, 古城文化
    activities: ['伊亚日落', '蓝顶教堂', '红沙滩', '火山岛游览', '卡马利黑沙滩']
  },
  { 
    id: 3, 
    name: '东京', 
    description: '现代与传统交融的都市', 
    image: 'https://img95.699pic.com/photo/50635/8283.jpg_wh860.jpg',
    price: '¥ 6,999',
    rating: 4.7,
    region: 1, // 亚洲
    themes: [2, 4, 5], // 古城文化, 美食之旅, 购物天堂
    activities: ['东京塔', '浅草寺', '明治神宫', '涩谷十字路口', '秋叶原']
  },
  { 
    id: 4, 
    name: '马尔代夫', 
    description: '梦幻般的海上天堂', 
    image: 'https://pic1.zhimg.com/v2-3128a1d1a90652fe5fb8ac0160aed12f_r.jpg',
    price: '¥ 15,999',
    rating: 4.9,
    region: 1, // 亚洲
    themes: [1], // 海岛度假
    activities: ['水上别墅体验', '珊瑚礁浮潜', '海底漫步', '海钓', '海滩烧烤']
  },
  { 
    id: 5, 
    name: '威尼斯', 
    description: '水城的浪漫与艺术', 
    image: 'https://n.sinaimg.cn/sinacn/400/w1200h800/20180603/872c-hcmurvh0052581.jpg',
    price: '¥ 9,999',
    rating: 4.6,
    region: 2, // 欧洲
    themes: [2, 4], // 古城文化, 美食之旅
    activities: ['贡多拉游船', '圣马可广场', '叹息桥', '里亚托桥', '穆拉诺岛']
  },
  { 
    id: 6, 
    name: '京都', 
    description: '日本传统文化的瑰宝', 
    image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.pyhV2tFkmGFjnDQORfiNCQHaD4?rs=1&pid=ImgDetMain',
    price: '¥ 7,500',
    rating: 4.8,
    region: 1, // 亚洲
    themes: [2, 3], // 古城文化, 自然风光
    activities: ['伏见稻荷大社', '清水寺', '金阁寺', '岚山竹林', '二条城']
  },
  { 
    id: 7, 
    name: '巴厘岛', 
    description: '热带天堂的度假胜地', 
    image: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.OhuY2Kp0lQPML_nCJLyLQAHaE8?rs=1&pid=ImgDetMain',
    price: '¥ 5,999',
    rating: 4.7,
    region: 1, // 亚洲
    themes: [1, 3], // 海岛度假, 自然风光
    activities: ['乌布皇宫', '海神庙', '库塔海滩', '圣猴森林', '乌布市场']
  },
  { 
    id: 8, 
    name: '纽约', 
    description: '不夜城的繁华与活力', 
    image: 'https://ts1.tc.mm.bing.net/th/id/R-C.a598f6cc9aac779ce8c8353d72605dd6?rik=IJSNY%2f3865USvw&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50059%2f1998.jpg_wh1200.jpg&ehk=78D%2bug3nBBgiRyqaJrof%2f4J06pHfnKWKKNBvvXzmWeA%3d&risl=&pid=ImgRaw&r=0',
    price: '¥ 11,999',
    rating: 4.6,
    region: 3, // 北美洲
    themes: [4, 5], // 美食之旅, 购物天堂
    activities: ['自由女神像', '中央公园', '帝国大厦', '百老汇', '时代广场']
  },
  { 
    id: 9, 
    name: '悉尼', 
    description: '南半球的独特魅力', 
    image: 'https://tse3-mm.cn.bing.net/th/id/OIP-C.huNZ3n8ByRjxRZpNgNPFeQHaE7?rs=1&pid=ImgDetMain',
    price: '¥ 12,500',
    rating: 4.7,
    region: 5, // 大洋洲
    themes: [1, 3], // 海岛度假, 自然风光
    activities: ['悉尼歌剧院', '悉尼海港大桥', '邦迪海滩', '蓝山国家公园', '塔龙加动物园']
  },
  { 
    id: 10, 
    name: '开罗', 
    description: '古埃及文明的瑰宝', 
    image: 'https://img1.qunarzz.com/travel/poi/1809/7f/9ec65f81ed43ae37.jpg_r_640x640_185030ae.jpg',
    price: '¥ 9,500',
    rating: 4.5,
    region: 6, // 非洲
    themes: [2], // 古城文化
    activities: ['金字塔', '狮身人面像', '埃及博物馆', '尼罗河游船', '哈利利市场']
  },
  { 
    id: 11, 
    name: '里约热内卢', 
    description: '热情奔放的海滨城市', 
    image: 'https://img95.699pic.com/photo/50497/7538.jpg_wh860.jpg',
    price: '¥ 14,800',
    rating: 4.6,
    region: 4, // 南美洲
    themes: [1, 3], // 海岛度假, 自然风光
    activities: ['基督山', '科帕卡巴纳海滩', '面包山', '桑巴舞表演', '马拉卡纳体育场']
  },
  { 
    id: 12, 
    name: '伦敦', 
    description: '历史与现代交织的都市', 
    image: 'https://ts1.tc.mm.bing.net/th/id/R-C.ab0226410fd275caf55597df53605f23?rik=BvuISQdNCl9Dgg&riu=http%3a%2f%2fn.sinaimg.cn%2fsinacn%2fw1920h1201%2f20180312%2f8c7a-fyscsmu5354744.jpg&ehk=BdZ%2fJE%2bAEJAuO6L0Gw64cgCP9kO1BSFVa%2fME7OnpvWs%3d&risl=&pid=ImgRaw&r=0',
    price: '¥ 10,500',
    rating: 4.7,
    region: 2, // 欧洲
    themes: [2, 5], // 古城文化, 购物天堂
    activities: ['大本钟', '伦敦眼', '大英博物馆', '白金汉宫', '塔桥']
  }
]);

// 筛选选项
const regions = ref([
  { id: 1, name: '亚洲' },
  { id: 2, name: '欧洲' },
  { id: 3, name: '北美洲' },
  { id: 4, name: '南美洲' },
  { id: 5, name: '大洋洲' },
  { id: 6, name: '非洲' }
]);

const themes = ref([
  { id: 1, name: '海岛度假' },
  { id: 2, name: '古城文化' },
  { id: 3, name: '自然风光' },
  { id: 4, name: '美食之旅' },
  { id: 5, name: '购物天堂' }
]);

// 搜索关键词
const searchKeyword = ref('');

// 筛选条件
const selectedRegions = ref<number[]>([]);
const selectedThemes = ref<number[]>([]);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(6);
const pageSizeOptions = ref([6, 9, 12]);

// 处理筛选条件变化
const handleRegionChange = (regionId: number) => {
  const index = selectedRegions.value.indexOf(regionId);
  if (index > -1) {
    selectedRegions.value.splice(index, 1);
  } else {
    selectedRegions.value.push(regionId);
  }
  currentPage.value = 1; // 重置为第一页
};

const handleThemeChange = (themeId: number) => {
  const index = selectedThemes.value.indexOf(themeId);
  if (index > -1) {
    selectedThemes.value.splice(index, 1);
  } else {
    selectedThemes.value.push(themeId);
  }
  currentPage.value = 1; // 重置为第一页
};

// 处理搜索
const handleSearch = () => {
  currentPage.value = 1; // 重置为第一页
};

// 处理页码变化
const handleCurrentPageChange = (val: number) => {
  currentPage.value = val;
};

// 处理每页展示数量变化
const handlePageSizeChange = (val: number) => {
  pageSize.value = val;
  currentPage.value = 1; // 重置为第一页
};

// 筛选方法
const filteredDestinations = computed(() => {
  let result = destinations.value;
  
  // 搜索关键词筛选
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase();
    result = result.filter(dest => 
      dest.name.toLowerCase().includes(keyword) || 
      dest.description.toLowerCase().includes(keyword)
    );
  }
  
  // 地区筛选
  if (selectedRegions.value.length > 0) {
    result = result.filter(dest => selectedRegions.value.includes(dest.region));
  }
  
  // 主题筛选
  if (selectedThemes.value.length > 0) {
    result = result.filter(dest => 
      dest.themes.some(themeId => selectedThemes.value.includes(themeId))
    );
  }
  
  return result;
});

// 分页后的目的地
const paginatedDestinations = computed(() => {
  const startIndex = (currentPage.value - 1) * pageSize.value;
  return filteredDestinations.value.slice(startIndex, startIndex + pageSize.value);
});

// 总页数
const totalItems = computed(() => filteredDestinations.value.length);

// 当筛选条件变化时，重置页码
watch([searchKeyword, selectedRegions, selectedThemes], () => {
  currentPage.value = 1;
});

// 查看详情
const viewDestinationDetail = (destId: number) => {
  router.push(`/destination/${destId}`);
};
</script>

<template>
  <div class="destination-page">
    <Header />
    
    <div class="destination-banner">
      <div class="banner-content">
        <h1>探索全球目的地</h1>
        <p>发现世界各地的美景与文化，开启您的下一段旅程</p>
      </div>
    </div>
    
    <div class="container">
      <div class="search-filter-section">
        <div class="search-bar">
          <input 
            type="text" 
            v-model="searchKeyword" 
            placeholder="搜索目的地..." 
            @keyup.enter="handleSearch"
          />
          <button class="search-button" @click="handleSearch">搜索</button>
        </div>
        
        <div class="filters">
          <div class="filter-group">
            <h3>按地区筛选</h3>
            <div class="filter-options">
              <label 
                v-for="region in regions" 
                :key="region.id" 
                class="filter-option"
                :class="{ 'active': selectedRegions.includes(region.id) }"
              >
                <input 
                  type="checkbox" 
                  :value="region.id" 
                  :checked="selectedRegions.includes(region.id)"
                  @change="handleRegionChange(region.id)" 
                />
                <span>{{ region.name }}</span>
              </label>
            </div>
          </div>
          
          <div class="filter-group">
            <h3>按主题筛选</h3>
            <div class="filter-options">
              <label 
                v-for="theme in themes" 
                :key="theme.id" 
                class="filter-option"
                :class="{ 'active': selectedThemes.includes(theme.id) }"
              >
                <input 
                  type="checkbox" 
                  :value="theme.id" 
                  :checked="selectedThemes.includes(theme.id)"
                  @change="handleThemeChange(theme.id)" 
                />
                <span>{{ theme.name }}</span>
              </label>
            </div>
          </div>
        </div>
      </div>
      
      <div class="destinations-header">
        <div class="result-count">
          共找到 <span class="count-number">{{ totalItems }}</span> 个目的地
        </div>
      </div>
      
      <div class="destinations-grid">
        <div v-for="dest in paginatedDestinations" :key="dest.id" class="destination-card">
          <div class="destination-image">
            <img :src="dest.image" :alt="dest.name" />
            <div class="destination-rating">
              <span class="star">★</span>
              <span>{{ dest.rating }}</span>
            </div>
          </div>
          <div class="destination-info">
            <h3 class="destination-name">{{ dest.name }}</h3>
            <p class="destination-desc">{{ dest.description }}</p>
            <div class="destination-activities">
              <h4>热门活动:</h4>
              <ul>
                <li v-for="(activity, index) in dest.activities.slice(0, 3)" :key="index">
                  {{ activity }}
                </li>
              </ul>
            </div>
            <div class="destination-footer">
              <div class="destination-price">{{ dest.price }} 起</div>
              <button class="view-details-btn" @click="viewDestinationDetail(dest.id)">查看详情</button>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:pageSize="pageSize"
          :page-sizes="pageSizeOptions"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalItems"
          @size-change="handlePageSizeChange"
          @current-change="handleCurrentPageChange"
          background
        />
      </div>
    </div>
    
    <Footer />
  </div>
</template>

<style scoped>
.destination-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.destination-banner {
  height: 300px;
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('https://images.unsplash.com/photo-1469854523086-cc02fe5d8800?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2021&q=80');
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
}

.banner-content h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
}

.banner-content p {
  font-size: 1.2rem;
  max-width: 600px;
  margin: 0 auto;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 15px;
}

.search-filter-section {
  margin-bottom: 30px;
}

.search-bar {
  display: flex;
  margin-bottom: 20px;
}

.search-bar input {
  flex: 1;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
  font-size: 16px;
}

.search-button {
  padding: 0 20px;
  background-color: #0066cc;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  font-size: 16px;
}

.filters {
  display: flex;
  gap: 30px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
}

.filter-group {
  flex: 1;
}

.filter-group h3 {
  margin-bottom: 15px;
  font-size: 18px;
  color: #333;
}

.filter-options {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.filter-option {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  padding: 5px 10px;
  background-color: white;
  border-radius: 20px;
  border: 1px solid #ddd;
  transition: all 0.2s;
}

.filter-option.active {
  background-color: #0066cc;
  color: white;
  border-color: #0066cc;
}

.destinations-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.result-count {
  font-size: 16px;
  color: #666;
}

.count-number {
  font-weight: bold;
  color: #0066cc;
}

.destinations-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 30px;
}

.destination-card {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  background-color: white;
  transition: transform 0.3s, box-shadow 0.3s;
}

.destination-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.destination-image {
  position: relative;
  height: 200px;
}

.destination-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.destination-rating {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: white;
  border-radius: 15px;
  padding: 3px 10px;
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
  font-weight: bold;
}

.star {
  color: #ffcc00;
}

.destination-info {
  padding: 20px;
}

.destination-name {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #333;
}

.destination-desc {
  color: #666;
  margin-bottom: 15px;
}

.destination-activities {
  margin-bottom: 20px;
}

.destination-activities h4 {
  font-size: 16px;
  margin-bottom: 8px;
  color: #555;
}

.destination-activities ul {
  padding-left: 20px;
  color: #666;
}

.destination-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
}

.destination-price {
  font-size: 18px;
  font-weight: bold;
  color: #0066cc;
}

.view-details-btn {
  padding: 8px 15px;
  background-color: #0066cc;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.view-details-btn:hover {
  background-color: #0055aa;
}

.pagination-container {
  margin-top: 40px;
  display: flex;
  justify-content: center;
}

/* 适配移动端 */
@media (max-width: 768px) {
  .filters {
    flex-direction: column;
    gap: 20px;
  }
  
  .destinations-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}
</style> 